//
//  ChiyouViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/13.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "ChiyouViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "Succeed.h"
#import "SucceedTableViewCell.h"
@interface ChiyouViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;

@end

@implementation ChiyouViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = @"已成功的投资";
    self.array = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSLog(@"+++++++++%@", self.strusername);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 120;
    [self.view addSubview:self.tableView];
    [self.tableView release];
//    NSString *str = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/account/BidSuccessList?strusername=%@", self.strusername];
    NSString *str = @"http://www.cloudpeng.com:8102/account/BidSuccessList?strusername=xuehairong";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            Succeed *model = [[Succeed alloc] initWithDictionary:dic];
            
            // self.userID = model.userid;
            
            [self.array addObject:model];
            
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"___________%@",error);
    }];
    
    [self.tableView reloadData];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SucceedTableViewCell *cell = [[SucceedTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        
    }
    cell.nameTitle.text = [[self.array objectAtIndex:indexPath.row] Title];
    cell.touzijine1.text = [NSString stringWithFormat:@"%@.00(元)", [[self.array objectAtIndex:indexPath.row] Available]];
    
    CGFloat a = [[[self.array objectAtIndex:indexPath.row] Available] integerValue];
    CGFloat b = [[[self.array objectAtIndex:indexPath.row] RootRate] integerValue];
    
    int c = a * b / 100;
   // NSLog(@"%f", c);
    
    NSString *time = [[self.array objectAtIndex:indexPath.row] BidTime];
    NSArray *timearray = [time componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
    cell.touziriqi1.text =[NSString stringWithFormat:@"%@", [timearray objectAtIndex:0]];
    NSString *time1 = [[self.array objectAtIndex:indexPath.row] BeginRateTime];
    NSArray *timearray1 = [time1 componentsSeparatedByString:@"T"]; //从字符A中分隔成2个元素的数组
    cell.jixi1.text = [NSString stringWithFormat:@"%@", [timearray1 objectAtIndex:0]];
    //创建日期格式化对象
    NSDateFormatter *dataFormatter = [[NSDateFormatter alloc] init];
    [dataFormatter setDateFormat:@"yyyy-MM-dd"];
    //创建了两个日期对象
    NSDate *date1 = [dataFormatter dateFromString:[timearray objectAtIndex:0]];
    NSDate *date2 = [dataFormatter dateFromString:[timearray1 objectAtIndex:0]];
    //取两个日期对像的时间间隔
    NSTimeInterval along = [date2 timeIntervalSinceDate:date1];
    
    int days1 = ((int)along) * 365 ;
    int days = days1 / (3600 * 24);
    // int hours = ((int)along) / (3600 * 24) / 3600;
    NSString *dateContent = [[NSString alloc] initWithFormat:@"%i天", days];
    NSLog(@"__________%@", dateContent);
   
    cell.qixian1.text = [NSString stringWithFormat:@"%@个月", [[self.array objectAtIndex:indexPath.row] Limit]];
    cell.lilv1.text = [NSString stringWithFormat:@"%@.00%%",[[self.array objectAtIndex:indexPath.row] RootRate]];
    cell.leixing1.text = [NSString stringWithFormat:@"%@",[[self.array objectAtIndex:indexPath.row] name]];
    cell.lixi1.text = [NSString stringWithFormat:@"%d.00元", c];
    cell.shengyu1.text = [NSString stringWithFormat:@"%@",dateContent];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
